package common.correspondence;


import com.google.protobuf.AbstractMessage;

import java.util.Arrays;
import java.util.HashMap;

// 定义消息格式
public class Message {
    public String source;
    public byte[] dataBytes;
    public String[] data;
    public MessageType type;
    public HashMap<String, String> dataMap;
    String msg;
    public AbstractMessage message;
    public long receiveTime;
    public long parseTime;
    public int index;

    public Message(MessageType type,String ... data){
        this.type = type;
        this.dataMap = new HashMap<>();
        this.dataMap.put("msg", Arrays.toString(data));
    }

    public Message(int type, AbstractMessage message){
        this.type = MessageType.getByIndex(type);
        this.message = message;
    }


    public Message(int type, byte[] bs){
        this.type = MessageType.getByIndex(type);
        dataBytes = bs;
    }



    public enum MessageType{
        Message,   Count, Verify, Restart ;

        public static MessageType getByIndex(int index) {
            for (MessageType c : MessageType.values()) {
                if (c.ordinal() == index) {
                    return c;
                }
            }
            return null;
        }
        }
}
